package xhwl.insurance.ui;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions.RxPermissions;

import de.greenrobot.event.EventBus;
import rx.functions.Action1;
import xhwl.android.common.util.CommonAppUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.upgrade.UpgradeEvent;
import xhwl.insurance.upgrade.UpgradeMgr;
import xhwl.insurance.util.PermisionUtils;
import xhwl.insurance.util.ToastUtil;

/**
 * Created by Administrator on 2017/8/24.
 * 版本说明
 */

public class VersionActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = VersionActivity.class.getSimpleName();

    private LinearLayout mFunction;
    private LinearLayout mCheckVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.version_about);
        EventBus.getDefault().register(VersionActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_function_introduction:
                startActivity(new Intent(this, VersionFunctionActivity.class));
                break;
            case R.id.ll_check_version:
                requestPermission();
                break;
            default:
                break;

        }
    }

    private void requestPermission() {
        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) {
                            new UpgradeMgr().checkVersion(VersionActivity.this);
                        } else {
                            Log.e(TAG, "用户拒绝开启存储");
                            PermisionUtils.dialog(VersionActivity.this, "读取手机存储");

                        }
                    }

                });
    }

    @Override
    public void initViews() {
        initTitleViews();
        String versionName = CommonAppUtil.getVersionName(getApplicationContext());
        mFunction = (LinearLayout) findViewById(R.id.ll_function_introduction);
        mCheckVersion = (LinearLayout) findViewById(R.id.ll_check_version);
        TextView mVersionName = (TextView) findViewById(R.id.tv_version_name);
        mVersionName.setText(versionName);

    }

    @Override
    public void initListener() {
        mFunction.setOnClickListener(this);
        mCheckVersion.setOnClickListener(this);


    }

    @Override
    protected void setUpTitleView() {
        titleTextView.setText("版本说明");
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    new UpgradeMgr().checkVersion(VersionActivity.this);
                } else {
                    PermisionUtils.dialog(VersionActivity.this, "读取手机存储");
                }
                break;
        }
    }

    public void onEvent(Object obj) {
        if (obj instanceof UpgradeEvent) {
            UpgradeEvent event = (UpgradeEvent) obj;
            if (event.isDownloadComplete()) {
                new UpgradeMgr().checkVersion(VersionActivity.this);
            } else if (event.isStartDownload()) {
                ToastUtil.showToast(this, "开始下载...");
            } else if (event.isNewest()) {
                ToastUtil.showToast(this, "当前已是最新版本");
            } else if (event.isDownloading()) {
                ToastUtil.showToast(this, "下载中...");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (null != PermisionUtils.alertDialog)
            PermisionUtils.alertDialog.dismiss();
    }
}
